#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<int> sortByBits(vector<int>& arr) {
        sort(arr.begin(), arr.end());
        vector<int> v;
        multimap<int, int> M;
        for (int i = 0; i < arr.size(); i++) {
            int t = arr[i];
            int count_1 = 0;
            while (t) {
                if (t % 2 == 1) {
                    count_1++;
                }
                t = t / 2;
            }
            M.insert(make_pair(count_1, arr[i]));
        }
        ;
        for (multimap<int, int>::iterator index = M.begin(); index!=M.end(); index++) {
            v.push_back(index->second);
        }
        return v;


    }
};


//int main() {
//    vector<int> f = { 1024,512,256,128,64,32,16,8,4,2,1 };
//    Solution S;
//    vector<int> T = S.sortByBits(f);
//    for (int i = 0; i < T.size(); i++) {
//        cout << T[i] << endl;
//    }
//}